<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :search.sync="search"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
      <template slot="menuLeft">
        <el-button type="success" size="small" icon="el-icon-upload2" v-if="permission.historyInventoryManage_import" @click="importConfig.show = true">导入</el-button>
      </template>
      <template slot-scope="{ row }" slot="menu">
        <el-button icon="el-icon-view" size="small" type="text" @click="rowView(row)">流水详情</el-button>
      </template>
    </avue-crud>
    <el-dialog v-dragDirectives ref="dialog" title="流水详情" append-to-body :visible.sync="viewDialog" :close-on-click-modal="false" top="5vh" width="50%">
      <h3 class="custom-title"><i class="el-icon-vertical-bar mr10"></i>物料信息</h3>
      <avue-form v-if="viewDialog" ref="partInfo" v-model="partInfo" :option="partInfoOption"></avue-form>
      <h3 class="custom-title"><i class="el-icon-vertical-bar mr10"></i>流水详情</h3>
      <avue-crud
        v-if="viewDialog"
        :option="detailOption"
        :table-loading="detailLoading"
        :data="detailData"
        :page.sync="detailPage"
        @search-change="detailSearchChange"
        @search-reset="detailSearchReset"
        @current-change="detailCurrentChange"
        @size-change="detailSizeChange"
        @refresh-change="detailRefreshChange"
      ></avue-crud>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="viewDialog = false"><i class="el-icon-circle-close"></i><span>取 消</span></el-button>
      </span>
    </el-dialog>
    <importDialogComp
      :title="importConfig.title"
      :url="importConfig.importUrl"
      :formData="importConfig.formData"
      :export-name="importConfig.exportName"
      :visible.sync="importConfig.show"
      @submit="codeClose"
    ></importDialogComp>
  </basic-container>
</template>

<script>
import importDialogComp from '@/components/dialog/import/index.vue';
import { getList, getDetailList, getPartBrandList } from '@/api/warehouse/history-inventory-manage';
import { mapGetters } from 'vuex';
import userTableConfig from '@/components/user-table-config/index.vue';
import userConfig from '@/mixins/userConfig';
export default {
  name: 'WarehouseHistoryInventoryManage',
  components: {
    importDialogComp,
    userTableConfig
  },
  mixins: [userConfig()],
  data() {
    return {
      form: {},
      query: {},
      search: {},
      fileList: [],
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      importConfig: {
        show: false,
        importUrl: '/api/mes-warehouse/history-inventory-lot/import',
        exportName: '历史库存导入模版.xlsx',
        formData: {
          req: []
        },
        title: '历史库存导入'
      },
      selectionList: [],
      option: {
        height: 'calc(100vh - 360px)',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        viewBtn: false,
        editBtn: false,
        addBtn: false,
        delBtn: false,
        menu: true,
        selection: true,
        columnBtn: false,
        dialogClickModal: false,
        column: [
          {
            label: '物料编码',
            prop: 'partNo',
            search: true
          },
          {
            label: '物料名称',
            prop: 'partName'
          },
          {
            label: '物料规格',
            prop: 'partSpec'
          },
          {
            label: '物料品牌',
            prop: 'partBrand',
            search: true,
            type: 'select',
            dicData: [],
            click: () => {
              this.getBrandList();
            },
            change: (val) => {
              let obj = this._find(this.partBrandList, { partBrand: val });
              if (obj) {
                this.search.partModel = obj.partModel;
              }
            }
          },
          {
            label: '物料型号',
            prop: 'partModel',
            search: true
          },
          {
            label: '内部批次',
            prop: 'lotNo'
          },
          {
            label: '导入数量',
            prop: 'quantity'
          },
          {
            label: '剩余库存数量',
            prop: 'remainingQuantity'
          }
        ]
      },
      data: [],
      rowId: '',
      viewDialog: false,
      partInfo: {},
      partInfoOption: {
        menuBtn: false,
        column: [
          {
            label: '物料编码',
            prop: 'partNo',
            disabled: true
          },
          {
            label: '物料名称',
            prop: 'partName',
            disabled: true
          },
          {
            label: '物料型号',
            prop: 'partModel',
            disabled: true
          },
          {
            label: '物料规格',
            prop: 'partSpec',
            disabled: true
          },
          {
            label: '物料品牌',
            prop: 'partBrand',
            disabled: true
          },
          {
            label: '内部批次',
            prop: 'lotNo',
            disabled: true
          },
          {
            label: '导入数量',
            prop: 'quantity',
            disabled: true
          }
        ]
      },
      detailOption: {
        height: 300,
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        viewBtn: false,
        editBtn: false,
        addBtn: false,
        delBtn: false,
        menu: false,
        selection: false,
        columnBtn: false,
        dialogClickModal: false,
        column: [
          {
            label: '导入数量',
            prop: 'changeQuantity'
          },
          {
            label: '操作人',
            prop: 'operationByName'
          },
          {
            label: '操作时间',
            prop: 'changeTime',
            search: true,
            searchSpan: 16,
            type: 'date',
            searchType: 'datetimerange',
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            startPlaceholder: '开始时间',
            endPlaceholder: '结束时间'
          }
        ]
      },
      detailLoading: false,
      detailData: [],
      detailPage: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      detailQuery: {},
      partBrandList: []
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  computed: {
    ...mapGetters(['permission']),
    permissionList() {
      return {};
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(',');
    }
  },
  methods: {
    codeClose(value) {
      if (value) {
        this.onLoad(this.page);
      }
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    rowView(row) {
      this.viewDialog = true;
      this.partInfo = row;
      this.rowId = row.id;
      this.queryDetail(this.detailPage);
    },
    queryDetail(page, params = {}) {
      this.detailQuery['historyInventoryLotId'] = this.rowId;
      this.detailLoading = true;
      getDetailList(page.currentPage, page.pageSize, Object.assign(params, this.detailQuery)).then((res) => {
        const data = res.data.data;
        this.detailPage.total = data.total;
        this.detailData = data.records;
        this.detailLoading = false;
      });
    },
    detailSearchChange(params, done) {
      this.detailQuery = params;
      this.detailPage.currentPage = 1;
      if (params.changeTime && params.changeTime.length > 0) {
        params.startTime = params.changeTime[0];
        params.endTime = params.changeTime[1];
        delete params.changeTime;
      }
      this.queryDetail(this.detailPage, params);
      done();
    },
    detailSearchReset() {
      this.detailQuery = {};
      this.queryDetail(this.detailPage);
    },
    detailCurrentChange(currentPage) {
      this.detailPage.currentPage = currentPage;
      this.queryDetail(this.detailPage);
    },
    detailSizeChange(pageSize) {
      this.detailPage.pageSize = pageSize;
      this.queryDetail(this.detailPage);
    },
    detailRefreshChange() {
      this.queryDetail(this.detailPage);
    },
    getBrandList() {
      getPartBrandList(this.search.partNo).then(
        (res) => {
          this.partBrandList = this._get(res, 'data.data', []);
          let partBrand = this.findObject(this.option.column, 'partBrand');
          partBrand.dicData = this.partBrandList.map((item) => {
            return {
              label: item.partBrand,
              value: item.partBrand
            };
          });
        },
        (error) => {
          this.partBrandList = [];
          partBrand.dicData = [];
        }
      );
    }
  }
};
</script>

<style></style>
